package com.ruoyi.teaching.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.page.PageDomain;
import com.ruoyi.common.core.web.page.TableSupport;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.teaching.bo.YaStudentIBo;
import com.ruoyi.teaching.domain.YaHoliday;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.YaStudentMapper;
import com.ruoyi.teaching.domain.YaStudent;
import com.ruoyi.teaching.service.IYaStudentService;
import org.springframework.transaction.annotation.Transactional;


/**
 * 学生信息Service业务层处理
 *
 * @author ZBX
 * @date 2025-03-10
 */
@Service
public class YaStudentServiceImpl extends ServiceImpl<YaStudentMapper, YaStudent> implements IYaStudentService {
    @Autowired
    private YaStudentMapper yaStudentMapper;
    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 查询学生信息
     *
     * @param id 学生信息主键
     * @return 学生信息
     */
    @Override
    public YaStudent selectYaStudentById(Long id) {
        return yaStudentMapper.selectById(id);
    }

    /**
     * 查询学生信息列表
     *
     * @param yaStudent 学生信息
     * @return 学生信息
     */
    @Override
    public List<YaStudent> selectYaStudentList(YaStudent yaStudent) {
//        QueryWrapper<YaStudent> queryWrapper = new QueryWrapper<>();
//        // 封装查询条件，包括用户名和名字
//        queryWrapper.like(yaStudent.getUsername() != null, "username2" , yaStudent.getUsername());
//        queryWrapper.like(yaStudent.getName() != null, "name" , yaStudent.getName());
        LambdaQueryWrapper<YaStudent> queryWrapper = new LambdaQueryWrapper<>();
        // 封装查询条件，包括用户名和名字
        queryWrapper.like(yaStudent.getUsername() != null, YaStudent::getUsername , yaStudent.getUsername());
        queryWrapper.like(yaStudent.getName() != null, YaStudent::getName , yaStudent.getName());

        // 用MybatisPlus的分页查询
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Page<YaStudent> page = new Page<>(pageNum, pageSize);
        Page<YaStudent> studentPage = super.page(page, queryWrapper);
        List<YaStudent> records = studentPage.getRecords();
        return records;
    }

    /**
     * 新增学生信息
     *
     * @param yaStudent 学生信息
     * @return 结果
     */
    @Override
    //@Transactional(transactionManager = "transactionManager")
    // 分布式事务的用法（与system模块中的注册方法处于同一事物中）
    @GlobalTransactional(rollbackFor = Exception.class)
    public int insertYaStudent(YaStudent yaStudent) {
        // 1.远程调用系统微服务注册账号，返回SysUser
        SysUser sysUser = new SysUser();
        sysUser.setNickName(yaStudent.getName());
        sysUser.setUserName(yaStudent.getUsername());
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        R<SysUser> sysUser2 = remoteUserService.registerStudent(sysUser, "inner");
        Long userId = sysUser2.getData().getUserId();
        // 2.新增学员信息（绑定学员id为user_id）
        yaStudent.setId(userId);
        //int i =1/0;
        return yaStudentMapper.insert(yaStudent);
    }

    /**
     * 修改学生信息
     *
     * @param yaStudent 学生信息
     * @return 结果
     */
    @Override
    public int updateYaStudent(YaStudent yaStudent) {
        return yaStudentMapper.updateById(yaStudent);
    }

    /**
     * 批量删除学生信息
     *
     * @param ids 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public int deleteYaStudentByIds(Long[] ids) {
        return yaStudentMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除学生信息信息
     *
     * @param id 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteYaStudentById(Long id) {
        return yaStudentMapper.deleteById(  id);
    }

    @Override
    public List<YaStudentIBo> selectYaStudentNameList(Long classesId) {
        return yaStudentMapper.selectYaStudentNameList(classesId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<YaStudent> selectYaStudentListByClassesId(Long classesId) {
        return yaStudentMapper.selectList(new LambdaQueryWrapper<YaStudent>().eq(YaStudent::getClassesId, classesId));
    }
}
